<?php
/**
* goods.php
*
* Create on 2017-2-17
* Create by TT
*/
namespace haibao\cms\business\newproduct;

use \haibao\cms\business\BusinessException;
use \haibao\cms\data\newproduct\Goods as GoodsData;

class Goods extends \haibao\cms\business\Base{

    public static $brandIdArr = array();
    
    /**
     * 根据goodsid获取imageid
     */
    public static function getImageIdByGoodsId($goodsId){
        $data = new GoodsData();
        $imageInfo = array();
        $imageId = $data->getImageIdByGoodsId($goodsId);
        if($imageId){
            if(is_array($goodsId)){
                $imageInfo = \haibao\cms\business\ImageLibrary::getImageUrlById($imageId);
                foreach($imageId as $key=>$val){
                    if(isset($imageInfo[$val])){
                        $imageId[$key] = $imageInfo[$val];
                    }else{
                        $imageId[$key] = '';
                    }
                }
                return $imageId;
            }else{
                $imageInfo = \haibao\cms\business\ImageLibrary::getImageById($imageId);
                return $imageInfo->Filename;
            }
            
        }else{
            return $imageInfo;
        }
    }
    /**
     * 用户添加单品评分
     */
    public static function addGoodsScore($userId,$goodsId,$score){
        if(empty($userId)){
            throw new BusinessException('用户ID不能为空');
        }
        if(empty($goodsId)){
            throw new BusinessException('单品ID不能为空');
        }
        if(empty($score)){
            throw new BusinessException('分数不能为空');
        }
        $data = new GoodsData();
        return $data->addGoodsScore($userId,$goodsId,$score);
    }
    /**
     * 根据用户ID提取单品评分
     */
    public static function getUserGoodsScore($goodsId,$userId){
        $data = new GoodsData();
        return $data->getUserGoodsScore($goodsId,$userId);
    }
    
    /**
     * 根据用户ID提取单品使用
     */
    public static function getUseCount($goodsId){
    	$data = new GoodsData();
    	return $data->getUseCount($goodsId);
    }
    
    /**
     * 根据用户ID提取单品使用
     */
    public static function addUseCount($goodsId){
    	$data = new GoodsData();
    	return $data->addUseCount($goodsId);
    }

    public static function getGoodsScore($goodsId){
        $data = new GoodsData();
        return $data->getGoodsScore($goodsId);
    }
    
    /**
     * 根据类型ID获取类型名称
     */
    public static function getCategoryNameById($categoryId){
        $data = new GoodsData();
        return $data->getCategoryNameById($categoryId);
    }
    
    /**
     * 根据goodid获取属性值
     */
    public static function getGoodsPropertyById($goodsId){
        $data = new GoodsData();
        return $data->getGoodsPropertyById($goodsId);
    }

    public static function getGoodsProperyByIds($goodsIds){
        $data = new GoodsData();
        return $data->getGoodsProperyByIds($goodsIds);
    }
    
    /**
     * 根据goodsis获取试用
     */
    public static function getTryOutByProductId($productId,$isOnTry = true){
        $data = new GoodsData();
        return $data->getTryOutByProductId($productId,$isOnTry);
    }
    
    /**
     * 根据品牌Id获取最新单品
     */
    public static function getNewProductByBrandId($brandId){
        $data = new GoodsData();
        return $data->getNewProductByBrandId($brandId);
    }
    
	/**
	 * 添加商品属性
	 * @param array(\haibao\cms\model\data\newproduct\GoodsProperty) $models
	 */
	public static function addGoodsPropertyModels($models){
		$data = new GoodsData();
		$data->addAll($models);
	}
	
	public static function addProduct($model,$picId){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($model)){
			throw new BusinessException('商品不能为空',self::DATA_EMPTY);
		}
		if(empty($model->ProductId)){
			throw new BusinessException('产品ID不能为空',self::DATA_ERROR_PRODUCT_ID);
		}
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->CreateTime = new \DateTime();
		
		$data = new GoodsData();
		$data->add($model);
		
		$goodsImageModel = new \haibao\cms\model\data\newproduct\GoodsImage();
		$goodsImageModel->ImageId = $picId;
		$goodsImageModel->GoodsId = $model->Id;
		$goodsImageModel->IsCover = \haibao\cms\model\data\newproduct\GoodsImage::IS_COVER_YES;
		$data->add($goodsImageModel);

		$data->addGoodsStatistic($model->Id);

		$imageData = new \haibao\cms\data\ImageLibrary();
		$imageData->updateImageStatus($picId,\haibao\cms\model\data\ImageLibrary::STATUS_ENABLE);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_ADD, $model->toArray(false));
	}
	
	public static function batchUpdateProduct($ids,$arrSet,$modelPropertyModes = array()){
		self::checkOperation(self::FUNC_CODE_PRODUCT_UPDATE);
	
		if(empty($arrSet)){
			throw new BusinessException('需要修改的数据不能为空',self::DATA_EMPTY);
		}
		if(empty($ids)){
			throw new BusinessException('商品ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
	
		$data = new GoodsData();
		$data->batchUpdateProduct($ids, $arrSet,$modelPropertyModes);
	
		$arrSet['id'] = implode(',', $ids);
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	public static function getOneModel($filter){
		$data = new GoodsData();
		return $data->getOne($filter);
	}
	
	public static function getModelList($query){
		$data = new GoodsData();
		return $data->getAll($query);
	}
	
	public static function updateProduct($id,$arrSet,$propertyModels,$picId = 0){
		self::checkOperation(self::FUNC_CODE_PRODUCT_ADD);
		if(empty($id)){
			throw new BusinessException('商品ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
		if(empty($arrSet)){
			throw new BusinessException('需要修改的数据不能为空',self::DATA_EMPTY);
		}
		$data = new GoodsData();
		$data->updateProduct($id, $arrSet,$propertyModels,$picId);
		
		$arrSet['Id'] = $id;
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_UPDATE, $arrSet);
	}
	
	public static function updateStatusById($id,$status = \haibao\cms\model\data\newproduct\Product::STATUS_ENABLE){
		self::checkOperation(self::FUNC_CODE_PRODUCT_DEL);
		if(empty($id)){
			throw new BusinessException('商品ID不能为空',self::DATA_ERROR_PRODUCT_MODEL_ID);
		}
		$goodsData = new GoodsData();
		$goodsData->updateStatusById($id,$status);
	
		$arrSet['Id'] = $id;
		$arrSet['Status'] = $status;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_DEL, $arrSet);
	}

	public static function getStatisticByGoodsId($goodsId){
        $goodsData = new GoodsData();
        return $goodsData->getStatisticByGoodsId($goodsId);
    }

    public static function updateCommentNum($goodsId,$num = 1,$isSub = false){
        $goodsData = new GoodsData();
        $goodsData->updateCommentNum($goodsId,$num,$isSub);
    }
    
    public static function getProductBrandInfo($property){
        $goodsData = new GoodsData();
        return $goodsData->getProductBrandInfo($property);
    }
    public static function getBrandPropertyId($brandId){
        $goodsData = new GoodsData();
        return $goodsData->getBrandPropertyId($brandId);
    }
    
    public static function getProductPorperyByCategoryIds($allCategoryArr){
        $goodsData = new GoodsData();
        return $goodsData->getProductPorperyByCategoryIds($allCategoryArr);
    }

    public static function getGoodsInfoByIds($idStr){
        $goodsData = new GoodsData();
        return $goodsData->getGoodsInfoByIds($idStr);
    }
}